/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package routeplanner.ui;

import routeplanner.algorithm.DifferentDijkstra;
import routeplanner.algorithm.DirectedGraph;
import routeplanner.database.RouteDAO;
import routeplanner.entities.Atraction;
import routeplanner.entities.City;
import routeplanner.entities.CityConnection;
import routeplanner.entities.Vehicle;

/**
 *
 * @author Damian
 */
public class MainWindow extends javax.swing.JFrame {

    private DirectedGraph graph;
    private RouteDAO route;
    /**
     * Creates new form MainWindow
     * @param graph
     */
    public MainWindow(DirectedGraph graph, RouteDAO route) {
        initComponents();
        this.graph = graph;
        this.route = route;
        //dodanie zmiennych z bazy do comboboxów
        for (City city : route.getCities()) {
            startCityComboBox.addItem(city.getName());
            endCityComboBox.addItem(city.getName());
        }
        for (Atraction atraction : route.getAtractions()) {
            atractionComboBox.addItem(atraction.getName());
        }
        for (Vehicle vehicle : route.getVehicles()) {
            vehicleComboBox.addItem(vehicle.getName());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        startCityComboBox = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        endCityComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        atractionComboBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        wysokoscCheckBox = new javax.swing.JCheckBox();
        generateRouteButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        wynikTextArea = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        vehicleComboBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Główne okno aplikacji");

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel1.setText("Miasto początkowe (skąd):");

        startCityComboBox.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel2.setText("Miasto końcowe (dokąd):");

        endCityComboBox.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel3.setText("Interesująca atrakcja:");

        atractionComboBox.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel4.setText("Czy wysokość n.p.m. jest istotna?");

        wysokoscCheckBox.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        wysokoscCheckBox.setText("tak/nie");

        generateRouteButton.setFont(new java.awt.Font("Times New Roman", 3, 14)); // NOI18N
        generateRouteButton.setText("Generuj trasę");
        generateRouteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateRouteButtonActionPerformed(evt);
            }
        });

        wynikTextArea.setColumns(20);
        wynikTextArea.setFont(new java.awt.Font("Times New Roman", 3, 12)); // NOI18N
        wynikTextArea.setRows(5);
        jScrollPane1.setViewportView(wynikTextArea);

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel5.setText("Wybrany pojazd:");

        vehicleComboBox.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(startCityComboBox, 0, 232, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(endCityComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(atractionComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(generateRouteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(wysokoscCheckBox, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(60, 60, 60)
                        .addComponent(vehicleComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(startCityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(endCityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(atractionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(vehicleComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(wysokoscCheckBox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(generateRouteButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void generateRouteButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        long distance = 0;
        DifferentDijkstra dijkstra = new DifferentDijkstra(graph,
                                        route.getCityByName(startCityComboBox.getSelectedItem().toString()),
                                        route.getAtractionByName(atractionComboBox.getSelectedItem().toString()),
                                        wysokoscCheckBox.isSelected());
        
        wynikTextArea.setText("Wybrany pojazd: " + vehicleComboBox.getSelectedItem().toString() + "\n");
        wynikTextArea.setText(wynikTextArea.getText() + "Droga z " + startCityComboBox.getSelectedItem().toString() + " do " + endCityComboBox.getSelectedItem().toString() + ":\n");
        if (dijkstra.getPath(route.getCityByName(startCityComboBox.getSelectedItem().toString()), route.getCityByName(endCityComboBox.getSelectedItem().toString())).isEmpty())
            wynikTextArea.setText(wynikTextArea.getText() + "Brak trasy lub jesteś już w tym mieście!\n");
        for (CityConnection cc : dijkstra.getPath(route.getCityByName(startCityComboBox.getSelectedItem().toString()), route.getCityByName(endCityComboBox.getSelectedItem().toString()))) {
            wynikTextArea.setText(wynikTextArea.getText() + "Z " + cc.getStartCity().getName() + " do " + cc.getEndCity().getName() + " (" + Math.abs(cc.getDistance()) +" km)\n");
            distance += Math.abs(cc.getDistance());
        }
        double kosztPrzejazdu;
        kosztPrzejazdu = distance*route.getVehicleByName(vehicleComboBox.getSelectedItem().toString()).getKmCost();
        wynikTextArea.setText(wynikTextArea.getText() + "Całkowita długość trasy: " + distance + " km\n");
        wynikTextArea.setText(wynikTextArea.getText() + "Średni koszt przejazdu pojazdem " + vehicleComboBox.getSelectedItem().toString() + " wynosi " + kosztPrzejazdu + " zł");
    }                                                   

    // Variables declaration - do not modify                     
    private javax.swing.JComboBox atractionComboBox;
    private javax.swing.JComboBox endCityComboBox;
    private javax.swing.JButton generateRouteButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox startCityComboBox;
    private javax.swing.JComboBox vehicleComboBox;
    private javax.swing.JTextArea wynikTextArea;
    private javax.swing.JCheckBox wysokoscCheckBox;
    // End of variables declaration                   
}